package com.siblings.loan.utils.dialog

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.lib.base.utils.DensityUtils
import com.lib.base.utils.ImageUtil
import com.siblings.lib_baseadapter.SIBOnItemChildClickListener
import com.siblings.lib_baseadapter.SIBRecyclerViewAdapter
import com.siblings.lib_baseadapter.SIBViewHolderHelper
import com.siblings.loan.R
import com.siblings.loan.databinding.DialogBindCardSubmitBinding
import com.siblings.loan.http.HttpHelper
import com.siblings.loan.utils.decoration.LinearSpacingItemDecoration
import com.siblings.loan.utils.event.SibEventType
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/18 13:45
 * @Annotation：绑卡确认dialog
 */
class BindCardSubmitDialog: ProDialog {
    private lateinit var binding: DialogBindCardSubmitBinding
    private var mCallback: IDialogCallback? = null

    private var itemAdapter: ItemAdapter? = null
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogBindCardSubmitBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        initRecyclerView()
    }

    override fun initListener() {
        setOnClickListener(R.id.imgClose)
        setOnClickListener(R.id.btnConfirm)
    }

    override fun initData() {
    }

    override fun onViewClick(v: View?) {
        dismiss()
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    /**
     * 初始化账户列表
     */
    private fun initRecyclerView() {
        val topDecoration = DensityUtils.dp2px(12f)
        binding.recyclerView.addItemDecoration(LinearSpacingItemDecoration(0, 0,0,topDecoration))
        binding.recyclerView.layoutManager = LinearLayoutManager(context)
        itemAdapter = ItemAdapter(binding.recyclerView)
        binding.recyclerView.adapter = itemAdapter!!
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        itemAdapter!!.setOnItemChildClickListener(SIBOnItemChildClickListener { _, childView, position ->
        })
    }

    /**
     * 设置账户列表
     */
    fun setBindAccountList(accountList: MutableList<JSONObject>){
        itemAdapter!!.data = accountList
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
    }

    class ItemAdapter(recyclerView: RecyclerView?) : SIBRecyclerViewAdapter<JSONObject>(recyclerView, R.layout.item_loan_account) {
        override fun setItemChildListener(helper: SIBViewHolderHelper, viewType: Int) {
            helper.setItemChildClickListener(R.id.itemLayout) //点击Item
        }
        override fun fillData(helper: SIBViewHolderHelper, position: Int, model: JSONObject) {
            helper.setText(R.id.textName,model.optString("walletDesc"))
            helper.setText(R.id.textNumber,"+" + model.optString("walletMobile"))
            val imgLogo = helper.getImageView(R.id.imgLogo)
            ImageUtil.instance!!.loadImageUrl(mContext,imgLogo,model.optString("walletLogo"), com.lib.base.R.mipmap.icon_holder)

            //不显示删除
            helper.setVisibility(R.id.deleteLayout,View.GONE)
        }
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addSibEventAction(SibEventType.DIALOG.SIB_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_SHOW,"")
    }

    override fun dismiss() {
        super.dismiss()
        HttpHelper.addSibEventAction(SibEventType.DIALOG.SIB_DIALOG_USER_ACCOUNT_SUBMIT_CONFIRM_TIPS_DISMISS,"")
    }
}